package com.mobilezu.mobile.android.mobile4s.activity;

import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;
import com.mobilezu.mobile.android.mobile4s.R;
import com.mobilezu.mobile.android.mobile4s.view.slidingmenu.fragment.MainContentFragment;
import com.mobilezu.mobile.android.mobile4s.view.slidingmenu.fragment.MainMenuFragment;
import com.mobilezu.mobile.android.mobile4s.view.slidingmenu.view.SlidingMenu;

/**
 * Main Activity
 * 
 * @author xiaoming.yuan
 * @create 2013-5-28
 * @version 1.0.0
 */
public class MainActivity extends FragmentActivity {
	private SlidingMenu mSlidingMenu;
	private MainMenuFragment mMenuFragment;
	private MainContentFragment mainContentFragment;

	@Override
	protected void onCreate(Bundle bundle) {
		super.onCreate(bundle);
		setContentView(R.layout.sliding_menu_layout);
		init();
	}

	/**
	 * Initialize view controller
	 */
	private void init() {
		mSlidingMenu = (SlidingMenu) findViewById(R.sliding_menu_layout.slidingMenu);
		mSlidingMenu.setLeftView(getLayoutInflater().inflate(
				R.layout.main_menu_layout_frame, null));
		mSlidingMenu.setCenterView(getLayoutInflater().inflate(
				R.layout.main_layout_frame, null));
		FragmentTransaction t = this.getSupportFragmentManager()
				.beginTransaction();
		mMenuFragment = new MainMenuFragment();
		t.replace(R.main_menu_layout_frame.menu_frame, mMenuFragment);
		mainContentFragment = new MainContentFragment();
		t.replace(R.main_layout_frame.main_frame, mainContentFragment);
		t.addToBackStack(null);
		t.commit();
	}

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);
	}

	/**
	 * Show left menu
	 */
	public void showLeft() {
		mSlidingMenu.showLeftView();
	}

	/**
	 * @see android.support.v4.app.FragmentActivity#onKeyDown(int,
	 *      android.view.KeyEvent)
	 */
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			Intent intent = new Intent(Intent.ACTION_MAIN);
			intent.addCategory(Intent.CATEGORY_HOME);
			startActivity(intent);
		}
		return super.onKeyDown(keyCode, event);
	}

}
